package com.leo;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.input.MouseButton;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;

/**
 * @ClassName MouseDemo
 * @Description
 * @Author wangss
 * @date 2020.06.04 22:37
 * @Version 1.0
 */
public class MouseDemo extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        AnchorPane an = new AnchorPane();
        an.setStyle("-fx-background-color: #e7eeea");
        Scene scene = new Scene(an);

        HBox hBox = new HBox(10);

        Button b1 = new Button("button1");
        Button b2 = new Button("button2");

        hBox.getChildren().addAll(b1, b2);

        AnchorPane.setTopAnchor(hBox, 100d);
        AnchorPane.setLeftAnchor(hBox, 100d);
        an.getChildren().add(hBox);
        primaryStage.setScene(scene);
        primaryStage.setTitle("ImageViewDemo");
        primaryStage.setWidth(600d);
        primaryStage.setHeight(600d);
        primaryStage.setResizable(false);
        primaryStage.show();

        b1.setOnMouseClicked(event -> {
            System.out.println(event.getX());
            System.out.println(event.getY());

            System.out.println(event.getSceneX());
            System.out.println(event.getSceneY());

            System.out.println(event.getScreenX());
            System.out.println(event.getScreenY());

            // 事件源
            System.out.println(event.getSource());
            // 事件类型
            System.out.println(event.getEventType());
            // 事件目标
            System.out.println(event.getTarget());
            // 点击得哪个键
            System.out.println(event.getButton());
            // 点击次数
            System.out.println(event.getClickCount());
            // 一个键压下，通过另一个键来判断是否按下（不好解释）
            System.out.println(event.isSecondaryButtonDown());

            if (event.getClickCount() == 2 && event.getButton().name().equals(MouseButton.PRIMARY.name())) {
                System.out.println("左键点击两次");
            }
        });

        b1.setOnMousePressed(event -> {
            System.out.println("点击");
        });

        b1.setOnMouseReleased(event -> {
            System.out.println("释放");
        });

        b1.setOnMouseEntered(event -> {
            System.out.println("鼠标进入");
        });

        b1.setOnMouseExited(event -> {
            System.out.println("鼠标退出");
        });

        b1.setOnMouseMoved(event -> System.out.println("鼠标移动" + event.getSceneX()));
    }
}
